#include<iostream>
#include"menu.h"
#include<string>
#define Max 1000
struct Person
{
	std::string m_name;
	std::string m_sex;
	int m_age;
	std::string m_phone;
	std::string m_adress;
};
struct AdressBooks
{
	Person personArray[Max];
	int m_size;
};
void Addpeople(AdressBooks* abs)
{
	if (abs->m_size == Max)
	{
		std::cout << "管理库人员已满" << std::endl;
		return;
	}
	else
	{
		std::string name;
		std::cout << "姓名：" << std::endl;;
		std::cin >> name;
		abs->personArray[abs->m_size].m_name = name;

		std::cout << "性别：" << std::endl;
		std::cout << "1. 男" << std::endl;
		std::cout << "2. 女" << std::endl;
		int sex = 0;
		while (true)
		{
			std::cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_size].m_sex = sex == 1 ? "男" : "女";
				break;
			}
			else
			{ std::cout << "请重新输入" << std::endl; }
		}
		int age;
		std::cout << "年龄：" << std::endl;;
		std::cin >> age;
		abs->personArray[abs->m_size].m_age = age;
		std::string phone;
		std::cout << "电话：" << std::endl;;
		std::cin >> phone;
		abs->personArray[abs->m_size].m_phone = phone;
		std::string adress;
		std::cout << "地址：" << std::endl;;
		std::cin >> adress;
		abs->personArray[abs->m_size].m_adress = adress;

		abs->m_size++;
		std::cout << "添加成功" << std::endl;
		system("cls");                         //清屏
	}
}
void ShowPerson(AdressBooks* abs)
{
	if (abs->m_size == 0)
	{
		std::cout << "当前记录为空" << std::endl;
	}
	else
	{
		for (int i = 0; i < abs->m_size; i++)
		{
			std::cout << "姓名：" << abs->personArray[i].m_name << "\t"
				<< "性别：" << abs->personArray[i].m_sex << "\t"
				<< "年龄：" << abs->personArray[i].m_age << "\t"
			    << "电话：" << abs->personArray[i].m_phone << "\t"
			    << "地址：" << abs->personArray[i].m_adress << std::endl;
		}
	}
	system("pause");
	system("cls");
}
int CheckPerson(AdressBooks* abs, std::string name)
{
	for (int i = 0; i < abs->m_size; i++)
	{
		if (abs->personArray[i].m_name == name)
		{  return i;   }
	}
	return -1;
}
void DelPerson(AdressBooks* abs)
{
	std::cout << "请输入想要删除的人名：" << std::endl;
	std::string name;
	std::cin >> name;
	int ret = CheckPerson(abs, name);
	if (ret != -1)
	{
		for (int i = ret; i < abs->m_size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_size--;
	}
	else
	{
		std::cout << "查无此人" << std::endl;
	}
	system("pause");
	system("cls");
}
void FindPerson(AdressBooks* abs)
{
	std::cout << "请输入想要查找的人名：" << std::endl;
	std::string name;
	std::cin >> name;
	int ret = CheckPerson(abs, name);
	if (ret != -1)
	{
		std::cout << "姓名：" << abs->personArray[ret].m_name << "\t"
			<< "性别：" << abs->personArray[ret].m_sex << "\t"
			<< "年龄：" << abs->personArray[ret].m_age << "\t"
			<< "电话：" << abs->personArray[ret].m_phone << "\t"
			<< "地址：" << abs->personArray[ret].m_adress << std::endl;
	}
	else
	{
		std::cout << "查无此人" << std::endl;
	}
	system("pause");
	system("cls");
}
void ModifyPerson(AdressBooks* abs)
{
	std::cout << "请输入您想要修改的姓名：" << std::endl;
	std::string name;
	std::cin >> name;
	int ret=CheckPerson(abs, name);
	if (ret != -1)
	{
		std::string name;
		std::cout << "姓名：" << std::endl;;
		std::cin >> name;
		abs->personArray[ret].m_name = name;
		std::string sex;
		std::cout << "性别：" << std::endl;
		std::cin >> sex;
		abs->personArray[ret].m_sex = sex;
		int age;
		std::cout << "年龄：" << std::endl;;
		std::cin >> age;
		abs->personArray[ret].m_age = age;
		std::string phone;
		std::cout << "电话：" << std::endl;;
		std::cin >> phone;
		abs->personArray[ret].m_phone = phone;
		std::string adress;
		std::cout << "地址：" << std::endl;;
		std::cin >> adress;
		abs->personArray[ret].m_adress = adress;
	}
	else
	{
		std::cout << "查无此人" << std::endl;
	}
	system("pause");
	system("cls");
}
void ClearPerson(AdressBooks* abs)
{
	abs->m_size = 0;
	std::cout << "通讯录已清空" << std::endl;
	system("pause");
	system("cls");
}
int main()
{
	AdressBooks abs;
	abs.m_size = 0;
	int value = 0;
	while (true)
	{
		showmenu();
		std::cin>>value;
		switch (value)
		{
		case 1:
			Addpeople(&abs);
			break;
		case 2:
			ShowPerson(&abs);
			break;
		case 3:
		/*{
			std::cout << "请输入您想要找的人：" << std::endl;
			std::string name;
			std::cin >> name;
			if ( CheckPerson(&abs, name) == -1 )
			{
				std::cout << "查无此人" << std::endl;
			}
			else
			{
				std::cout << "找到此人" << std::endl;
			}
		}*/
			DelPerson(&abs);
			break;
		case 4:
			FindPerson(&abs);
			break;
		case 5:
			ModifyPerson(&abs);
			break;
		case 6:
			ClearPerson(&abs);
			break;
		case 0:
			std::cout << "欢迎下次使用" << std::endl;
			system("pause");
			break;
		default:
			break;
		}
	}
}